var express = require('express'),
    json = require('./movies.json'),
    router = express.Router(),
    _ = require('underscore'),
    request = require('request');

module.exports = function (app) {
    router.get('/test', function (req, res) {
        var data = {
            name: "jjaiyy",
            website: "www.jjaiyy.com"
        }
        res.json(data);
    });

    router.get('/', function (req, res) {
        res.json(json);
    });

    router.post('/', function (req, res) {
        if (req.body.Id && req.body.Title && req.body.Director &&
            req.body.Year && req.body.Rating) {
            res.json(json);
        } else {
            res.json(500, {err: "错误！"});
        }
    });

    router.put('/:id', function (req, res) {
        if (req.params.id && req.body.Title && req.body.Director &&
            req.body.Year && req.body.Rating) {
            _.each(json, function (elem, index) {
                if (elem.Id === req.params.id) {
                    elem.Title = req.body.Title;
                    elem.Director = req.body.Director;
                    elem.Year = req.body.Year;
                    elem.Rating = req.body.Rating;

                }
            });
            res.json(json);
        }else{
            res.json(500, {err: "错误！"})
        }
    });

    router.delete('/:id', function (req, res) {
        var indexToDel = -1;
        _.each(json, function (elem, index) {
            if(elem.Id === req.params.id){
                indexToDel = index;
                return false;
            }
        });
        if(~indexToDel){
            json.splice(indexToDel, 1);
        }
        res.json(json);
    });

    router.get('/external-api', function (req, res) {
        request({
            method: 'GET',
            uri: 'http://localhost:' + (process.env.PORT || 3500),
        }, function (error, response, body) {
            if(error){throw error;}
            
            var movies = [];
            _.each(JSON.parse(body), function (elem, index) {
                movies.push({
                    Title: elem.Title,
                    Rating: elem.Rating
                });
            });
            res.json(_.sortBy(movies, 'Rating').reverse());
        });
    });

    router.get('/imdb', function (req, res) {
        request({
            method: 'GET',
            uri: 'http://sg.media-imdb.com/suggests/a/aliens.json'
        }, function (err, response, body) {
            console.log(body);
            var data = body.substring(body.indexOf('(') + 1);

            data = JSON.parse(data.substring(0, data.length - 1));
            var related = [];
            _.each(data.d, function (movie, index) {
                related.push({
                    Title: movie.l,
                    Year: movie.y,
                    Poster: movie.i?movie.i[0] : ''
                });
            });
            res.json(related);
        })
    })
    app.use(router);
}
